package servlet;

import service.BooksService;
import service.impl.BooksServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/StatisticsServlet")
public class StatisticsServlet extends HttpServlet {
    ResultSet rs;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        BooksService booksService = new BooksServiceImpl();
        String opr = req.getParameter("opr");

        int uid = Integer.parseInt(req.getParameter("uid"));
        int sid = Integer.parseInt(req.getParameter("sid"));
        int aid = 0;
        double amount = 0;
        BooksService service = new BooksServiceImpl();


        if (opr.equals("today")){
            rs = service.selectToday(uid,sid);
            try {
                while (rs.next()){
                    aid = rs.getInt("aid");
                    amount = rs.getInt("amount");
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            out.print("aid="+aid);
            out.print("amount="+amount);
            System.out.println(aid+":"+amount);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
